package ch.unisi.inf.java.project.riskbami.model;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;

/**
 * This class handles the artifical intelligence of the game.
 * 
 * @author minellir@lu.unisi.ch
 * @author babazadm@lu.unisi.ch
 */
public final class AI extends Player
{

    private Random random;

    /**
     * Constructor for the A.I.
     */
    public AI(String name, Color color) {
        super(name, color);
        super.isHuman = false;
        random = new Random();
    }
    
    
    /**
     * Makes play the game to the artificial intelligence
     */
    public void playTurn() {
        int numberOfOwnedCountries = this.getOwnedCountries().size();
        
        if(game.getTurnPhase() == 1 && !game.getIsFinished()) {
            if(!isInitialPlacing()){
                 int armies = this.getPerTurnArmies();
                 for(int i = 0; i < armies; i++) {
                    int randomNumber = random.nextInt(numberOfOwnedCountries);
                    PlaceArmiesCommand place = new PlaceArmiesCommand(game, this.getOwnedCountries().get(randomNumber), 1);
                                if(place.canExecute()){
                                    place.execute();
                                }
                            }
             } else {
                int randomNumber = random.nextInt(numberOfOwnedCountries);
                PlaceArmiesCommand place = new PlaceArmiesCommand(game, this.getOwnedCountries().get(randomNumber), 1);
                        if(place.canExecute()){
                            place.execute();
                        }
                    }
                }
                
          if(game.getTurnPhase() == 2) {
                int newNumber = random.nextInt(numberOfOwnedCountries);
                Country startingCountry = this.getOwnedCountries().get(newNumber);
                
                ArrayList<Country> nearbyCountries = startingCountry.getNearbyCountries();
                
                Country targetCountry = null;
                
                int minArmies = Integer.MAX_VALUE;
                
                for(Country c : nearbyCountries){
                    if(!isCountryInAICountryList(c) && c.getNumberOfArmies() < minArmies){
                        targetCountry = c;
                    }
                }
                
                if(targetCountry != null && startingCountry.getNumberOfArmies() > 2){
                    AttackCommand attack = new AttackCommand(game, startingCountry, targetCountry);
                        if(attack.canExecute()){
                            attack.execute();
                        }
                    }
                game.switchNextPlayer();
            }
        }
        
    
    
    /**
     * Useful methods to make the AI work
     */
    private boolean isCountryInAICountryList(Country country) {
        for(Country c : this.getOwnedCountries()) {
            if(c.getName().equals(country.getName()))
            return true;
        }
        return false;
    }
            
                
}
